<!DOCTYPE html>
<html>
<head>
	<title>定时任务管理</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
	    var path = window.document.location.href;
		var pathName = window.document.location.pathname;
		var hostPath = path.substring(0, path.indexOf(pathName));
		var projectName = pathName.substring(0, pathName.substr(1).indexOf("/") + 1);
		document.write('<scr' + 'ipt src="' + projectName + '/js/html/header.js"></scr'+'ipt>');
    </script>
</head>
<body>
	<table id="dg" style="width: auto; height: auto" fitColumns="true">
		<thead>
			<tr>
				<th field="id">ID</th>
				<th field="jobName" width="100"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>任务名称</th>
				<th field="jobStatus" width="40"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>任务状态</th>
				<th field="processFunction" width="100"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>执行函数</th>
				<th field="triggerType" width="40"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>触发方式</th>
				<th field="cronExpression" width="80"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>表达式</th>
				<th field="repeatTime" width="40"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>间隔时间</th>
				<th field="repeatCount" width="40"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>重复次数</th>
				<th field="startTime" width="40"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>开始时间</th>
				<th field="endTime" width="40"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>结束时间</th>
				<th field="lastRunTime"  width="100"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>上次执行时间</th>
				<th field="nextFireTime"  width="100"  align="right"
					data-options="
					sortable:true,
					remoteSort:false"
					>下次执行时间</th>
				<th field="op"  width="100"  align="center" 
					data-options="
					sortable:true,
					formatter:formatterOper"
					>操作</th>
			</tr>
		</thead>
	</table>
	<div id="dg-buttons" style="height: auto">
		<div style="margin-bottom:5px">
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" 	onclick="dlgAdd()">新增</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" 	onclick="remove()">删除</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" 	onclick="dlgEdit()">修改</a>
		</div>
		<div>
			<span>任务编号:</span>
			<input id="jobNo" type="text" name="jobNo" class="easyui-validatebox" value="" size=10></input>
			<span>任务说明:</span>
			<input id="jobDesc" type="text" name="jobDesc" class="easyui-validatebox" value="" size=10></input>
			<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" onclick="dgop.find()">查询</a>
		</div>
	</div>

	<div id="dlg-buttons">
		<a href="javascript:dlgSave()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
		<a href="javascript:dlgClose()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
	</div>

	<div id="dlg" class="easyui-dialog" closed="true" style="width:730px;height:280px;padding:10px 10px;" buttons="#dlg-buttons">
		<form method="post" id="fm">
			<table cellspacing="8px;">
				<td><input id="id" name="id" style="display:none" required="true" ></td>
				<tr>
					<td>任务名称：</td>
					<td>
						<input type="text" id="jobName" name="jobName"
							   class="easyui-validatebox" required="true" >&nbsp;
						<span style="color: red">*</span>
					</td>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
					<td>触发方式：</td>
					<td>
						<input type="text" id="triggerType" name="triggerType"
							   class="easyui-combobox" required="true" />&nbsp;
						<span style="color: red">*</span>
					</td>
				</tr>
				<tr>
					<td>执行函数：</td>
					<td>
						<input type="text" id="processFunction" name="processFunction"
							   class="easyui-validatebox" required="true" >&nbsp;
					</td>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
					<td>表达式：</td>
					<td>
						<input type="text" id="cronExpression" name="cronExpression"
							   class="easyui-validatebox" />&nbsp;
					</td>
				</tr>
				<tr>
					<td>间隔时间：</td>
					<td>
						<input type="text" id="repeatTime" name="repeatTime"
							   class="easyui-validatebox" >&nbsp;
					</td>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
					<td>重复次数：</td>
					<td>
						<input type="text" id="repeatCount" name="repeatCount"
							   class="easyui-validatebox" />&nbsp;
					</td>
				</tr>
				<tr>
					<td>开始时间：</td>
					<td>
						<input type="text" id="startTime" name="startTime"
							   class="easyui-validatebox" />&nbsp;
					</td>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
					<td>结束时间：</td>
					<td>
						<input type="text" id="endTime" name="endTime"
							   class="easyui-validatebox" />&nbsp;
					</td>
				</tr>
			</table>
		</form>
	</div>
	<script>
		var initd = {
			queryDataURL : "/system/job/query",//查询URL
			saveDataURL : "/system/job/save",//保存URL
			dg : $('#dg'),
			dataDicList:{'2':['runtime'],'STATUS':['jobStatus'],'TriggerType':['triggerType']}
		};

		function remove() {
			var selectedRows = $("#dg").datagrid("getSelections");
			if (selectedRows.length == 0) {
				$.messager.alert("系统提示", "请选择要删除的数据！");
				return;
			}
			var removeData = selectedRows[0]
			if (selectedRows.length > 1) {
				removeData = {
					'deleteRows':deleteRows,
				}
			}

			var jobId = removeData.id;

			$.messager.confirm("系统提示", "您确定要删除这<font color=red>" + selectedRows.length + "</font>条数据吗？", function(r) {
				if (r) {
					$.ajax({
						type:"DELETE",
						dataType:"json",
						url : projectName+"/system/job/"+jobId+"/remove",
						beforeSend : function() {
						},
						success : function(result) {
							if (result.success) {
								$.messager.alert("系统提示", "数据已成功删除！");
								$("#dg").datagrid("reload");
							} else {
								if(!jQuery.isEmptyObject(result.message)){
									$.messager.alert("系统错误", result.message);
								}else{
									$.messager.alert("系统错误", result);
								}
								return;
							}
						},
						error:function(err, status){
							errorMsgAlert(err);
							$.messager.progress("close");
						}
					});

				}
			});
		}
 

		function formatterOper(val, row, index) {
			return '[<a href="#" onclick="startJob(\'' + row.id + '\',\'' + row.jobName + '\',\'' + row.jobGroup + '\')">启动</a>]  '
				  +'[<a href="#" onclick="stopJob(\'' + row.id + '\',\'' + row.jobName + '\',\'' + row.jobGroup + '\')">停止</a>]  '
				  +'[<a href="#" onclick="triggerJob(\'' + row.id + '\',\'' + row.jobName + '\',\'' + row.jobGroup + '\')">立即执行</a>]';
					
		}

		function startJob(jobId, jobName,jobGroup) {
			$.ajax({
				type : "POST",
				url : projectName+"/system/job/start",
				data : JSON.stringify({
					'id' : jobId,
					'jobName' : jobName,
					'jobGroup': jobGroup
				}),
				dataType : "json",
				contentType : "application/json;charset=utf-8",
				success : function(data) {
					if (!data.success) {
						alert(data.message);
						return;
					}
					if (data != null) {
						initd.dg.datagrid('reload');
					}
				}
			});
		}

		function stopJob(jobId, jobName,jobGroup) {
			$.ajax({
				type : "POST",
				url : projectName+"/system/job/stop",
				data : JSON.stringify({
					'id' : jobId,
					'jobName' : jobName,
					'jobGroup': jobGroup
				}),
				dataType : "json",
				contentType : "application/json;charset=utf-8",
				success : function(data) {
					if (!data.success) {
						alert(data.message);
						return;
					}
					if (data != null) {
						initd.dg.datagrid('reload');
					}
				}
			});
		}

		function triggerJob(jobId, jobName,jobGroup) {
			if (!confirm("立即执行定时任务" + jobName + "？")) {
				return;
			}
			$.ajax({
				type : "POST",
				url : projectName+"/system/job/trigger",
				data : JSON.stringify({
					'id' : jobId,
					'jobName' : jobName,
					'jobGroup': jobGroup
				}),
				dataType : "json",
				contentType : "application/json;charset=utf-8",
				success : function(data) {
					if (!data.success) {
						alert(data.message);
						return;
					}
					if (data != null) {
						initd.dg.datagrid('reload');
					}
				}
			});
		}
	</script>
</body>
</html>

